package com.scaffold.card.action;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.card.entity.Category;
import com.scaffold.card.service.CategoryService;
import com.scaffold.commons.config.AbstractConfig;
import com.scaffold.commons.json.JacksonUtil;
import com.scaffold.upload.UploadHandle;

/**
 * 目录操作接口(json)
 * @author wangjg
 */
@Controller
public class CategoryJsonAction extends BasicBizAction{
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private AbstractConfig config;
	/**
	 * 目录列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/category/list.json")
	public String list(String keyword){
		Page page = this.getPage(20);
		List<Category> list = categoryService.pageQuery(page, keyword, null);
		return this.getPageResult(page, list);
	}

	/**
	 * 获取目录
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/category/get.json")
	public String get(Long id){
		Category category = categoryService.getCategory(id);
		this.checkOwner(category);
		return this.getSuccessResult(category);
	}

	/**
	 * 保存目录
	 */
	@RequestMapping("/category/save.json")
	public String save(Long id){
		String path = this.handleAffix("/card/category");
		
		if(id==null){
			Category category = new Category();
			this.bindParams(category, "enabled");
			if(path!=null){
				category.setPhoto(path);
			}
			this.setOwner(category);
			this.categoryService.save(category);
		}else{
			Category category = this.categoryService.getCategory(id);
			this.checkOwner(category);
			this.bindParams(category, "enabled");
			if(path!=null){
				category.setPhoto(path);
			}
			this.categoryService.update(category);
		}
		
		return this.getSuccessResult();
	}
	
	/*
	 * 处理附件
	 */
	private String handleAffix(String destDir){
		Map<String,String> upParams = getUploadParams();
		if(upParams==null){
			return null;
		}
		upParams.put("destDir", destDir);
		UploadHandle uh = new UploadHandle();
		String fileservice = config.getString("url.fileservice");
		uh.setUrl(fileservice);
		return uh.confirm(upParams);
	}
	
	private Map<String,String> getUploadParams(){
		Map<String,String> params = this.getParameterMap();
		String photo_params = params.get("photo_upload");
		if(photo_params==null||photo_params.length()==0){
			return null;
		}
		Map<String,String> upParams = (Map<String, String>) new JacksonUtil().jsonToMap(photo_params);
		return upParams;
	} 

	/**
	 * 删除目录
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/category/delete.json")
	public String delete(Long id){
		Category category = categoryService.getCategory(id);
		this.checkOwner(category);
		this.categoryService.delete(category);
		return this.getSuccessResult();
	}

	/**
	 * 获取目录列表
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/category/select.json")
	public String select(String keyword){
		List<Category> list = categoryService.pageQuery(null, keyword, null);
		return this.getSuccessResult(list);
	}
	
	/**
	 * 启用/禁用目录
	 * @param id 编号
	 * @param enabled 是否启用
	 * @return
	 */
	@RequestMapping("/category/enable.json")
	public String enable(Long id, String enabled){
		if(!"Y".equals(enabled)){
			enabled = "N";
		}
		Category category = categoryService.getCategory(id);
		this.checkOwner(category);
		category.setEnabled(enabled);
		this.categoryService.update(category);
		return this.getSuccessResult(enabled);
	}

}
